package com.uniplaza.app.volley;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.components.volley.EventCallback;
import com.android.volley.components.volley.GsonRequest;
import com.android.volley.components.volley.ResultObj;
import com.google.gson.Gson;
import com.uniplaza.app.utils.Utils;

import java.util.Map;

/**
 * 网络请求工具类
 * Created by Kim on 16/6/6.
 */
class Controller {
    private final String TAG = Controller.class.getSimpleName();
    private static Controller instance;

    public static final int PROCESS_FAIL = -1;  // 网络请求失败
    private static final int PROCESS_OK = 200;   // 网络请求成功

    /**
     * 超时时间
     */
    private static final int IMAGE_TIMEOUT_MS = 10000;

    /**
     * 图片上传 超时时间
     */
//    private static final int IMAGE_TIMEOUT_IMG = 60000;

    /**
     * 重新请求次数
     */
    private static final int IMAGE_MAX_RETRIES = 0;

    /**
     * Default backoff multiplier for image requests
     */
    private static final float IMAGE_BACKOFF_MULT = 0f;


    public static Controller getInstance() {
        if (instance == null) {
            instance = new Controller();
        }
        return instance;
    }

    void getData(RequestQueue mQueue, final int method, String url, final Map<String, String> headers, Map<String, String> params, final String nRequestType, final EventCallback mCallBack) {
        Utils.logError(TAG, "url --> " + url);
        Response.Listener<String> listener = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Utils.log(TAG, "getData " + nRequestType + " --> " + response);
                ResultObj obj = Utils.jsonToBean(ResultObj.class, response);
                if (null != obj) {
                    if (PROCESS_OK == obj.getState()) {
                        mCallBack.successNetWork(method, nRequestType, new Gson().toJson(obj.getResult()));
                    } else {
                        mCallBack.errorNetWork(nRequestType, obj.getState(), obj.getMsg());
                    }
                } else {
                    mCallBack.errorNetWork(nRequestType, PROCESS_FAIL, "");
                }
            }
        };

        Response.ErrorListener errorListener = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                volleyError.printStackTrace();
                mCallBack.errorNetWork(nRequestType, PROCESS_FAIL, "");
            }
        };

        GsonRequest request = new GsonRequest(method, url, headers, params, listener, errorListener);
        request.setRetryPolicy(new DefaultRetryPolicy(IMAGE_TIMEOUT_MS, IMAGE_MAX_RETRIES, IMAGE_BACKOFF_MULT));
        mQueue.add(request);
    }



//    void getWXData(RequestQueue mQueue, final int method, String url, final Map<String, String> headers, Map<String, String> params, final String nRequestType, final EventCallback mCallBack) {
//        Utils.logError(TAG, "url --> " + url);
//        Response.Listener<String> listener = new Response.Listener<String>() {
//            @Override
//            public void onResponse(String response) {
//                Utils.log(TAG, "getWXData " + nRequestType + " --> " +  response);
//                mCallBack.successNetWork(method, nRequestType, response);
//
//            }
//        };
//
//        Response.ErrorListener errorListener = new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError volleyError) {
//                volleyError.printStackTrace();
//                mCallBack.errorNetWork(nRequestType, PROCESS_FAIL, "");
//            }
//        };
//
//        GsonRequest request = new GsonRequest(method, url, headers, params, listener, errorListener);
//        request.setRetryPolicy(new DefaultRetryPolicy(IMAGE_TIMEOUT_MS, IMAGE_MAX_RETRIES, IMAGE_BACKOFF_MULT));
//        mQueue.add(request);
//    }




}
